package com.rjwh.dingdong.client.activity.download.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.DowloadUpdateListAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.DownloadUpdatePresenter;
import com.rjwh.dingdong.client.mvp.view.DownloadUpdateView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：whrjwh-parent
 * 类描述：本地可更新
 * 创建人：wby
 * 创建时间：2016/6/23 14:31
 * 修改人：wby
 * 修改时间：2016/6/23 14:31
 * 修改备注：
 *
 */
public class DownloadUpdateFragment extends BaseFragment implements DownloadUpdateView {
    @BindView(R.id.comm_listview)
    ListView commListview;
    @BindView(R.id.comm_RefreshView)
    SwipyRefreshLayout commRefreshView;

    private DownloadUpdatePresenter presenter;
    // TODO: Rename and change types and number of parameters
    private DowloadUpdateListAdapter adapter;

    public static DownloadUpdateFragment newInstance() {
        DownloadUpdateFragment fragment = new DownloadUpdateFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.comm_refresh_listview, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new DownloadUpdatePresenter(getActivity(), this);
        initView();
        commRefreshView.doPullRefreshing(500);
    }
    public void initView(){
        commRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                presenter.getServerData();
            }
        });
        adapter = new DowloadUpdateListAdapter(getActivity()) ;
        commListview.setAdapter(adapter);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        commRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        commRefreshView.setRefreshing(refreshing);
    }

    public void loadData() {
        adapter.clear();
        List<LocalGmaeInfo> list = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "isUpdate=1");
        adapter.appendToList(list);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        presenter.detachView();
        super.onDestroy();
    }
}
